const app = getApp()
const Host = require('../../config').Host
Page({
    /**
     * 页面的初始数据
     */
    data: {
        successShow: false,
        text: '公告公告公告公告公告公告公告公告公告公告',
        text2: '公告2公告2公告2公告2公告2公告2公告2公告2',
        marqueePace: 1, //滚动速度
        marqueeDistance: 0, //初始滚动距
        marqueeDistance2: 0,
        // marquee2copy_status: false,
        // marquee2_margin: 60,
        size: 14,
        orientation: 'left', //滚动方向
        interval: 25, // 时间间隔
        interval2: 20,
        goodsIndex: 0,
        isScroll: true,
        mengShow: false,
        tanShow: false,
        count: 1, //初始选择商品的数量
        standardIndex: -1, //初始选择规格的下标
        product: [],
        product2:null,
        searchShow: false,
        search_value: '',
        historySearch: [],
    },
    // 选择规格
    chooseStandardIndex: function(e) {
        var index = e.currentTarget.id;
        this.setData({
            standardIndex: index,
        })
    },
    getCount: function(e) {
        this.setData({
            count: e.detail.value,
        })
    },
    getSearchValue: function(e) {
        this.setData({
            search_value: e.detail.value,
        })
    },
    search: function (e) {
      var that = this
      var keyword = that.data.search_value
      wx.navigateTo({
        url: '/pages/goods_classify/goods_classify?keyword=' + keyword,
      })
    },
    del: function() {
        var that = this;
        var count = that.data.count;
        count--;
        if (count < 1) {
            count = 1;
            wx.showToast({
                title: '至少选择一个商品',
                icon: 'none',
            })
        }
        that.setData({
            count: count,
        })
    },
    add: function(e) {
        var that = this;
        var count = that.data.count;
        var buynum = e.currentTarget.dataset.buynum;
        if (count >= buynum) {
            count = buynum;
        } else {
            count++;
        }
        that.setData({
            count: count,
        })
    },
    buyShow: function(e) {
        var index = e.currentTarget.id;
        var that = this;
        var product=that.data.product;
        var product2 = product[index];
        that.setData({
            product2: product2,
        })
        wx.showToast({
            title: '加载中',
            icon:'loading',
            duration:500,
        })
       setTimeout(function(){
           that.setData({
               goodsIndex: index,
               mengShow: true,
               tanShow: true,
               isScroll: false,
           })
       },500)
    },
    closeShow: function() {
        this.close();
    },
    close: function() {
        var that = this;
        that.setData({
            mengShow: false,
            tanShow: false,
            count: 1,
            standardIndex: -1,
            isScroll: true,
        })
    },
    close2: function() {
        this.setData({
            searchShow: false,
        })
    },
    openSearch: function() {
        this.setData({
            searchShow: true,
            isScroll: false,
        })
    },
    addCart: function() {
        var that = this;
        var openid = wx.getStorageSync('openid');
        var product=that.data.product;
        var goodsIndex = that.data.goodsIndex;
        var standardIndex = that.data.standardIndex;
        var arr = [];
        var obj = {};
        obj.goods_id = product[that.data.goodsIndex].id;
        obj.standard_id = '0';
        if(product[that.data.goodsIndex].standard.length>0){
            if (that.data.standardIndex == -1) {
                wx.showToast({
                    title: '您还没有选择规格',
                    icon: 'none',
                })
            } else {
                obj.standard_id = product[goodsIndex].standard[standardIndex].id;
                obj.goods_count = that.data.count;
                arr.push(obj);
                obj = null;
                wx.request({
                  url: Host + 'Default/addcart',
                  data: { openid, item: JSON.stringify(arr) },
                  method: 'post',
                  header: { 'content-type': 'application/x-www-form-urlencoded' },
                  success: function (res) {
                    if (res.data.code == 200) {
                      that.close();
                      wx.showToast({
                        title: '加入购物车成功',
                        icon: 'success',
                      })
                    } else {
                      wx.showToast({
                        title: res.data.errmsg,
                        icon: 'none',
                      })
                    }
                  },
                })
            }
        }else{
            obj.goods_count = that.data.count;
            arr.push(obj);
            obj = null;
            //console.log(arr)
            wx.request({
              url: Host + 'Default/addcart',
              data: { openid,item: JSON.stringify(arr)},
              method: 'post',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              success: function (res) {
                if (res.data.code==200) {
                  that.close();
                  wx.showToast({
                    title: '加入购物车成功',
                    icon: 'success',
                  }) 
                } else {
                  wx.showToast({
                    title: res.data.errmsg,
                    icon: 'none',
                  })
                }
              },
            })
        }
       
    },
    onceBuy: function(){
        var that = this;
        var product = that.data.product;
        var goodsIndex=that.data.goodsIndex;
        var standardIndex=that.data.standardIndex;
        var arr=[];
        var obj={};
        obj.goods_id = product[that.data.goodsIndex].id;
        obj.standard_id='0';
        if (product[goodsIndex].standard.length>0){
            if (standardIndex==-1){
                wx.showToast({
                    title: '您还没有选择规格',
                    icon: 'none',
                })
            }else{
                obj.standard_id=product[goodsIndex].standard[standardIndex].id;
                obj.goods_count = that.data.count;
                arr.push(obj);
                obj = null;
                //console.log(arr)
                wx.setStorageSync('order', arr)
                that.close();
                wx.navigateTo({
                    url: '/pages/order/order',
                })
            }
        }else{
            obj.goods_count = that.data.count;
            arr.push(obj);
            obj = null;
            //console.log(arr)
            wx.setStorageSync('order', arr)
            that.close();
            wx.navigateTo({
                url: '/pages/order/order',
            })
        }  
    },
    getHistoryValue: function(e) {
        var index = e.currentTarget.id;
        var that = this;
        var search_value = that.data.historySearch[index];
        that.setData({
            search_value: search_value,
        })
        that.search();
    },
    deleteHistory: function() {
        var that = this;
              
        var openid = wx.getStorageSync('openid');
        wx.request({
          url: Host + 'Default/emptyhistory',
          data: { openid },
          success: function (res) {
            if (res.data.code==200) {
              var historySearch = {};
              that.setData({
                historySearch: historySearch,
              })
            }
          },
        })
    },
    run1: function () {
        var that = this;
        var interval = setInterval(function () {
            if (-that.data.marqueeDistance < that.data.length) {
                that.setData({
                    marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
                });

            } else {
                clearInterval(interval);
                that.setData({
                    marqueeDistance: that.data.windowWidth * 0.853
                });
                that.run1();
            }

        }, that.data.interval);

    },
    run2: function () {
        var that = this;
        var interval2 = setInterval(function () {
            if (-that.data.marqueeDistance2 < that.data.length2) {
                that.setData({
                    marqueeDistance2: that.data.marqueeDistance2 - that.data.marqueePace,
                });

            } else {
                clearInterval(interval2);
                that.setData({
                    marqueeDistance2: that.data.windowWidth * 0.853
                });
                that.run2();
            }

        }, that.data.interval2);

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // 页面显示
        var that = this;
        var length = that.data.text.length * that.data.size; //文字长度
        var length2 = that.data.text2.length * that.data.size;
        var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
        that.setData({
            length: length,
            length2: length2,
            windowWidth: windowWidth,
            // marquee2_margin: length < windowWidth ? windowWidth - length : that.data.marquee2_margin //当文字长度小于屏幕长度时，需要增加补白
        });

        // that.run1(); // 水平一行字滚动完了再按照原来的方向滚动
        // that.run2();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
       var that=this;
        var openid = wx.getStorageSync('openid');
        var recommend = "popular,banner";
        wx.request({
            url: Host + 'Default/goodsList',
            data: { openid,recommend},
            success: function(res) {
                if (res.data.list.length > 0) {
                    that.setData({
                        successShow: true, //数据加载完成显示
                    })
                    var query = wx.createSelectorQuery();
                    //选择id
                    query.select('#search').boundingClientRect()
                    query.exec(function (res) {
                        //res就是 所有标签为mjltest的元素的信息 的数组
                        console.log(res);
                        //取高度
                        console.log(res[0].height);
                    })
                }
                that.setData({
                    product: res.data.list,
                });
            },
        })
        //历史搜索
      wx.request({
        url: Host + 'Default/search',
        data: { openid},
        success: function (res) {
          that.setData({
            historySearch: res.data.list,
          });
        },
      })
      if(that.data.successShow){
        
      }
     
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        // var that = this;
        // var product = that.data.product;
        // var data=[
        //     {
        //         id: '0',
        //         img: '/images/goods-1.png',
        //         title:'新增商品名称商品名商品名称商品名称商品名称',
        //         price: '88.00',
        //         standard: [
        //             {
        //                 value: '45g装规格1',
        //                 price: '88.00',
        //             },
        //             {
        //                 value: '45g装规格2',
        //                 price: '128.00',
        //             },
        //             {
        //                 value: '45g装规格3',
        //                 price: '188.00',
        //             },
        //         ],
        //     },
        //     {
        //         id: '0',
        //         img: '/images/goods-1.png',
        //         title:'新增商称商品名称商品名称商品名称商品名称',
        //         price: '88.00',
        //         standard: [
        //             {
        //                 value: '45g装规格1',
        //                 price: '88.00',
        //             },
        //             {
        //                 value: '45g装规格2',
        //                 price: '128.00',
        //             },
        //             {
        //                 value: '45g装规格3',
        //                 price: '188.00',
        //             },
        //         ],
        //     }
        // ];
        // if(!that.data.mengShow&&!that.data.searchShow){
        //     var newProduct = product.concat(data);
        //     console.log(newProduct)
        //     that.setData({
        //         product: newProduct,
        //     })
        // }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})